<template>
    <div>
      <div style="padding: 10px 0;">
      <el-input style="width: 200px;" placeholder="请输入姓名搜索" v-model="orderName" suffix-icon="el-icon-search" >
      </el-input>
      <el-button type="primary" style="margin-left: 10px;" @click="load()"> 搜索</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="reset()"> 重置</el-button>
      </div>
      <div style="padding: 10px 0;">
      <el-button type="primary" style="margin-left: 10px;" @click="add()"> 新增</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="delAll()"> 批量删除</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="exportExcel()"> 导出</el-button>
      <el-upload
        accept = ".xls,.xlsx"
        :action='upload.url'
        :on-success = "handleFileSuccess"
        :show-file-list = "false"
        style="display: inline-block;">
       <el-button type="primary"  style="margin-left: 10px;"> 导入</el-button>
      </el-upload>
      </div>

<el-dialog title="新增商品" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="100px" :model="form" size="small">
         <el-form-item label="商品名称" >
            <el-input v-model="form.orderName" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="商品产地" >
            <el-input v-model="form.orderMap" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="商品库存(箱)" >
            <el-input v-model="form.orderCount" autocomplete="off"></el-input>
         </el-form-item>

    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addOrder()" >确 定</el-button>
  </div>
</el-dialog>

<el-dialog title="修改商品" :visible.sync="editFormVisible" width="30%">
    <el-form label-width="100px" :model="form" size="small">
         <el-form-item label="商品名称" >
            <el-input v-model="form.orderName" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="商品产地" >
            <el-input v-model="form.orderMap" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="商品库存(箱)" >
            <el-input v-model="form.orderCount" autocomplete="off"></el-input>
         </el-form-item>
    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="editOrder()" >确 定</el-button>
  </div>
</el-dialog>

<el-table :data="tableData" :border="true" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>

        <el-table-column prop="id" label="编号" v-if="false">
        </el-table-column>

        <el-table-column prop="orderName" label="商品名称">
        </el-table-column>

        <el-table-column prop="orderMap" label="商品产地" >
        </el-table-column>

        <el-table-column prop="orderCount" label="商品库存(箱)">
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
         <template slot-scope="scope">
             <el-button type="text" size="small" @click="edit(scope.row)">修改</el-button>
           <el-popconfirm
           style="margin-left: 5px;"
           confirm-button-text='好的'
           cancel-button-text='不用了'
           icon="el-icon-info"
           icon-color="red"
           @confirm="del(scope.row)"
           title="您确定删除吗？">
             <el-button type="text" slot="reference" size="small">删除</el-button>
           </el-popconfirm>
         </template>
        </el-table-column>
</el-table>

<div class="block" style="padding: 20px 0;">
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="pageNum"
    :page-sizes="[2, 5, 10, 15]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total=total>
  </el-pagination>
</div>
   </div>
</template>
<script>
import { getOrderListByPost, save, update, delOrder, exp } from '../api/Order.js'

export default {
  data () {
    return {
      tableData: [],
      multipleSelection: [],
      flag: false,
      asidWith: '',
      iconFold: 'el-icon-s-fold',
      total: 0,
      pageNum: 1,
      pageSize: 2,
      orderName: '',
      dialogFormVisible: false,
      editFormVisible: false,
      form: {
        id: '',
        orderName: '',
        orderMap: '',
        orderCount: ''
      },
      upload: {
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/excel/import'
      }
    }
  },
  created () {
    this.load();
  },
  computed: {
  },
  methods: {
    load () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        orderName: this.orderName
      }
      getOrderListByPost(params).then(res => {
        this.tableData = res.data
        this.total = res.total
      }).catch(err => {
        console.log(err)
      })
    },

    reset () {
      this.orderName = '';
      this.load();
    },
    resetForm () {
      this.form = {}
    },
    handleSizeChange (pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize
      this.load();
    },
    handleCurrentChange (pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum
      this.load();
    },
    add () {
      this.form = {}
      this.dialogFormVisible = true
    },
    del (row) {
      delOrder(row).then(res => {
        this.$message.success('删除成功')
        this.load()
      })
    },
    edit (row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.editFormVisible = true
    },
    editOrder () {
      update(this.form).then(res => {
        this.$message.success('修改成功')
      })
      this.editFormVisible = false
      this.load()
    },
    search () {
      console.log('搜索')
    },
    addOrder () {
      save(this.form).then(res => {
        this.$message.success('添加成功')
      })
      this.dialogFormVisible = false
      this.resetForm()
      this.load()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    delAll () {
      this.multipleSelection.forEach(item => {
        delOrder(item).then(res => {
          this.$message.success('删除成功')
          this.load()
        })
      })
    },
    exportExcel () {
      exp().then(res => {
        console.log(res)
      })
    },
    handleFileSuccess (response) {
      console.log(response)
      this.$message.success('导入成功')
      this.load()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
